package com.lzb.permanentcalendar.fragment;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.UtilsTransActivity;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.lzb.permanentcalendar.BaseNoModelFragment;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.activity.SelCityActivity;
import com.lzb.permanentcalendar.basic.utils.CityUtils;
import com.lzb.permanentcalendar.basic.utils.DataUtils;
import com.lzb.permanentcalendar.bean.AqiForecastRespBean;
import com.lzb.permanentcalendar.bean.CityBean;
import com.lzb.permanentcalendar.bean.Hours24Bean;
import com.lzb.permanentcalendar.bean.LiveIndexRespBean;
import com.lzb.permanentcalendar.bean.XQ7Bean;
import com.lzb.permanentcalendar.bean.ZlBean;
import com.lzb.permanentcalendar.databinding.FragmentWeatherBinding;
import com.lzb.permanentcalendar.dialog.PermissionExplainDialog;
import com.lzb.permanentcalendar.dialog.ReqPermissionDialog;
import com.lzb.permanentcalendar.utils.OkhttpUtils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.DoubleSummaryStatistics;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link WeatherFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class WeatherFragment extends BaseNoModelFragment<FragmentWeatherBinding> implements View.OnClickListener {

    private List<ImageView> imgList1;
    private List<ImageView> imgList2;
    private List<TextView> textViewsList1;
    private List<TextView> textViewsList2;
    private List<TextView> textViewsList3;
    private List<TextView> textViewsList4;
    private List<TextView> textViewsList5;
    private List<TextView> textViewsList6;
    private List<TextView> textViewsList7;
    private List<TextView> textViewsList8;
    private List<TextView> textViewsList9;
    private List<TextView> textViewsList10;


    @Override
    protected int onCreate() {
        return R.layout.fragment_weather;
    }

    @Override
    protected void initView() {
        mDataBinding.tvLocation.setOnClickListener(this);

        textViewsList1 = new ArrayList<>();
        textViewsList2 = new ArrayList<>();
        textViewsList3 = new ArrayList<>();
        textViewsList4 = new ArrayList<>();
        textViewsList5 = new ArrayList<>();
        textViewsList6 = new ArrayList<>();
        textViewsList7 = new ArrayList<>();
        textViewsList8 = new ArrayList<>();
        textViewsList9 = new ArrayList<>();
        textViewsList10 = new ArrayList<>();
        imgList1 = new ArrayList<>();
        imgList2 = new ArrayList<>();

        imgList1.add(mDataBinding.ivLight1);
        imgList1.add(mDataBinding.ivLight2);
        imgList1.add(mDataBinding.ivLight3);
        imgList1.add(mDataBinding.ivLight4);
        imgList1.add(mDataBinding.ivLight5);
        imgList1.add(mDataBinding.ivLight6);
        imgList1.add(mDataBinding.ivLight7);
        imgList1.add(mDataBinding.ivLight8);
        imgList1.add(mDataBinding.ivLight9);
        imgList1.add(mDataBinding.ivLight10);
        imgList1.add(mDataBinding.ivLight11);
        imgList1.add(mDataBinding.ivLight12);
        imgList1.add(mDataBinding.ivLight13);
        imgList1.add(mDataBinding.ivLight14);
        imgList1.add(mDataBinding.ivLight15);
        imgList1.add(mDataBinding.ivLight16);

        imgList2.add(mDataBinding.ivNight1);
        imgList2.add(mDataBinding.ivNight2);
        imgList2.add(mDataBinding.ivNight3);
        imgList2.add(mDataBinding.ivNight4);
        imgList2.add(mDataBinding.ivNight5);
        imgList2.add(mDataBinding.ivNight6);
        imgList2.add(mDataBinding.ivNight7);
        imgList2.add(mDataBinding.ivNight8);
        imgList2.add(mDataBinding.ivNight9);
        imgList2.add(mDataBinding.ivNight10);
        imgList2.add(mDataBinding.ivNight11);
        imgList2.add(mDataBinding.ivNight12);
        imgList2.add(mDataBinding.ivNight13);
        imgList2.add(mDataBinding.ivNight14);
        imgList2.add(mDataBinding.ivNight15);
        imgList2.add(mDataBinding.ivNight16);

        textViewsList1.add(mDataBinding.tvWeather1);
        textViewsList1.add(mDataBinding.tvWeather2);
        textViewsList1.add(mDataBinding.tvWeather3);
        textViewsList1.add(mDataBinding.tvWeather4);
        textViewsList1.add(mDataBinding.tvWeather5);
        textViewsList1.add(mDataBinding.tvWeather6);
        textViewsList1.add(mDataBinding.tvWeather7);
        textViewsList1.add(mDataBinding.tvWeather8);
        textViewsList1.add(mDataBinding.tvWeather9);
        textViewsList1.add(mDataBinding.tvWeather10);
        textViewsList1.add(mDataBinding.tvWeather11);
        textViewsList1.add(mDataBinding.tvWeather12);
        textViewsList1.add(mDataBinding.tvWeather13);
        textViewsList1.add(mDataBinding.tvWeather14);
        textViewsList1.add(mDataBinding.tvWeather15);
        textViewsList1.add(mDataBinding.tvWeather16);

        textViewsList2.add(mDataBinding.tvWeatherNight1);
        textViewsList2.add(mDataBinding.tvWeatherNight2);
        textViewsList2.add(mDataBinding.tvWeatherNight3);
        textViewsList2.add(mDataBinding.tvWeatherNight4);
        textViewsList2.add(mDataBinding.tvWeatherNight5);
        textViewsList2.add(mDataBinding.tvWeatherNight6);
        textViewsList2.add(mDataBinding.tvWeatherNight7);
        textViewsList2.add(mDataBinding.tvWeatherNight8);
        textViewsList2.add(mDataBinding.tvWeatherNight9);
        textViewsList2.add(mDataBinding.tvWeatherNight10);
        textViewsList2.add(mDataBinding.tvWeatherNight11);
        textViewsList2.add(mDataBinding.tvWeatherNight12);
        textViewsList2.add(mDataBinding.tvWeatherNight13);
        textViewsList2.add(mDataBinding.tvWeatherNight14);
        textViewsList2.add(mDataBinding.tvWeatherNight15);
        textViewsList2.add(mDataBinding.tvWeatherNight16);

        textViewsList3.add(mDataBinding.tvWind1);
        textViewsList3.add(mDataBinding.tvWind2);
        textViewsList3.add(mDataBinding.tvWind3);
        textViewsList3.add(mDataBinding.tvWind4);
        textViewsList3.add(mDataBinding.tvWind5);
        textViewsList3.add(mDataBinding.tvWind6);
        textViewsList3.add(mDataBinding.tvWind7);
        textViewsList3.add(mDataBinding.tvWind8);
        textViewsList3.add(mDataBinding.tvWind9);
        textViewsList3.add(mDataBinding.tvWind10);
        textViewsList3.add(mDataBinding.tvWind11);
        textViewsList3.add(mDataBinding.tvWind12);
        textViewsList3.add(mDataBinding.tvWind13);
        textViewsList3.add(mDataBinding.tvWind14);
        textViewsList3.add(mDataBinding.tvWind15);
        textViewsList3.add(mDataBinding.tvWind16);

        textViewsList4.add(mDataBinding.tvWindLevel1);
        textViewsList4.add(mDataBinding.tvWindLevel2);
        textViewsList4.add(mDataBinding.tvWindLevel3);
        textViewsList4.add(mDataBinding.tvWindLevel4);
        textViewsList4.add(mDataBinding.tvWindLevel5);
        textViewsList4.add(mDataBinding.tvWindLevel6);
        textViewsList4.add(mDataBinding.tvWindLevel7);
        textViewsList4.add(mDataBinding.tvWindLevel8);
        textViewsList4.add(mDataBinding.tvWindLevel9);
        textViewsList4.add(mDataBinding.tvWindLevel10);
        textViewsList4.add(mDataBinding.tvWindLevel11);
        textViewsList4.add(mDataBinding.tvWindLevel12);
        textViewsList4.add(mDataBinding.tvWindLevel13);
        textViewsList4.add(mDataBinding.tvWindLevel14);
        textViewsList4.add(mDataBinding.tvWindLevel15);
        textViewsList4.add(mDataBinding.tvWindLevel16);

        textViewsList5.add(mDataBinding.tvD1);
        textViewsList5.add(mDataBinding.tvD2);
        textViewsList5.add(mDataBinding.tvD3);
        textViewsList5.add(mDataBinding.tvD4);
        textViewsList5.add(mDataBinding.tvD5);
        textViewsList5.add(mDataBinding.tvD6);
        textViewsList5.add(mDataBinding.tvD7);
        textViewsList5.add(mDataBinding.tvD8);
        textViewsList5.add(mDataBinding.tvD9);
        textViewsList5.add(mDataBinding.tvD10);
        textViewsList5.add(mDataBinding.tvD11);
        textViewsList5.add(mDataBinding.tvD12);
        textViewsList5.add(mDataBinding.tvD13);
        textViewsList5.add(mDataBinding.tvD14);
        textViewsList5.add(mDataBinding.tvD15);
        textViewsList5.add(mDataBinding.tvD16);

        textViewsList6.add(mDataBinding.tvDate1);
        textViewsList6.add(mDataBinding.tvDate2);
        textViewsList6.add(mDataBinding.tvDate3);
        textViewsList6.add(mDataBinding.tvDate4);
        textViewsList6.add(mDataBinding.tvDate5);
        textViewsList6.add(mDataBinding.tvDate6);
        textViewsList6.add(mDataBinding.tvDate7);
        textViewsList6.add(mDataBinding.tvDate8);
        textViewsList6.add(mDataBinding.tvDate9);
        textViewsList6.add(mDataBinding.tvDate10);
        textViewsList6.add(mDataBinding.tvDate11);
        textViewsList6.add(mDataBinding.tvDate12);
        textViewsList6.add(mDataBinding.tvDate13);
        textViewsList6.add(mDataBinding.tvDate14);
        textViewsList6.add(mDataBinding.tvDate15);
        textViewsList6.add(mDataBinding.tvDate16);

        textViewsList7.add(mDataBinding.tvHour1);
        textViewsList7.add(mDataBinding.tvHour2);
        textViewsList7.add(mDataBinding.tvHour3);
        textViewsList7.add(mDataBinding.tvHour4);
        textViewsList7.add(mDataBinding.tvHour5);
        textViewsList7.add(mDataBinding.tvHour6);
        textViewsList7.add(mDataBinding.tvHour7);
        textViewsList7.add(mDataBinding.tvHour8);
        textViewsList7.add(mDataBinding.tvHour9);
        textViewsList7.add(mDataBinding.tvHour10);
        textViewsList7.add(mDataBinding.tvHour11);
        textViewsList7.add(mDataBinding.tvHour12);
        textViewsList7.add(mDataBinding.tvHour13);
        textViewsList7.add(mDataBinding.tvHour14);
        textViewsList7.add(mDataBinding.tvHour15);
        textViewsList7.add(mDataBinding.tvHour16);
        textViewsList7.add(mDataBinding.tvHour17);
        textViewsList7.add(mDataBinding.tvHour18);
        textViewsList7.add(mDataBinding.tvHour19);
        textViewsList7.add(mDataBinding.tvHour20);
        textViewsList7.add(mDataBinding.tvHour21);
        textViewsList7.add(mDataBinding.tvHour22);
        textViewsList7.add(mDataBinding.tvHour23);
        textViewsList7.add(mDataBinding.tvHour24);
        textViewsList7.add(mDataBinding.tvHour25);

        textViewsList8.add(mDataBinding.tvHourWeather1);
        textViewsList8.add(mDataBinding.tvHourWeather2);
        textViewsList8.add(mDataBinding.tvHourWeather3);
        textViewsList8.add(mDataBinding.tvHourWeather4);
        textViewsList8.add(mDataBinding.tvHourWeather5);
        textViewsList8.add(mDataBinding.tvHourWeather6);
        textViewsList8.add(mDataBinding.tvHourWeather7);
        textViewsList8.add(mDataBinding.tvHourWeather8);
        textViewsList8.add(mDataBinding.tvHourWeather9);
        textViewsList8.add(mDataBinding.tvHourWeather10);
        textViewsList8.add(mDataBinding.tvHourWeather11);
        textViewsList8.add(mDataBinding.tvHourWeather12);
        textViewsList8.add(mDataBinding.tvHourWeather13);
        textViewsList8.add(mDataBinding.tvHourWeather14);
        textViewsList8.add(mDataBinding.tvHourWeather15);
        textViewsList8.add(mDataBinding.tvHourWeather16);
        textViewsList8.add(mDataBinding.tvHourWeather17);
        textViewsList8.add(mDataBinding.tvHourWeather18);
        textViewsList8.add(mDataBinding.tvHourWeather19);
        textViewsList8.add(mDataBinding.tvHourWeather20);
        textViewsList8.add(mDataBinding.tvHourWeather21);
        textViewsList8.add(mDataBinding.tvHourWeather22);
        textViewsList8.add(mDataBinding.tvHourWeather23);
        textViewsList8.add(mDataBinding.tvHourWeather24);
        textViewsList8.add(mDataBinding.tvHourWeather25);

        textViewsList9.add(mDataBinding.tvHourWindLevel1);
        textViewsList9.add(mDataBinding.tvHourWindLevel2);
        textViewsList9.add(mDataBinding.tvHourWindLevel3);
        textViewsList9.add(mDataBinding.tvHourWindLevel4);
        textViewsList9.add(mDataBinding.tvHourWindLevel5);
        textViewsList9.add(mDataBinding.tvHourWindLevel6);
        textViewsList9.add(mDataBinding.tvHourWindLevel7);
        textViewsList9.add(mDataBinding.tvHourWindLevel8);
        textViewsList9.add(mDataBinding.tvHourWindLevel9);
        textViewsList9.add(mDataBinding.tvHourWindLevel10);
        textViewsList9.add(mDataBinding.tvHourWindLevel11);
        textViewsList9.add(mDataBinding.tvHourWindLevel12);
        textViewsList9.add(mDataBinding.tvHourWindLevel13);
        textViewsList9.add(mDataBinding.tvHourWindLevel14);
        textViewsList9.add(mDataBinding.tvHourWindLevel15);
        textViewsList9.add(mDataBinding.tvHourWindLevel16);
        textViewsList9.add(mDataBinding.tvHourWindLevel17);
        textViewsList9.add(mDataBinding.tvHourWindLevel18);
        textViewsList9.add(mDataBinding.tvHourWindLevel19);
        textViewsList9.add(mDataBinding.tvHourWindLevel20);
        textViewsList9.add(mDataBinding.tvHourWindLevel21);
        textViewsList9.add(mDataBinding.tvHourWindLevel22);
        textViewsList9.add(mDataBinding.tvHourWindLevel23);
        textViewsList9.add(mDataBinding.tvHourWindLevel24);
        textViewsList9.add(mDataBinding.tvHourWindLevel25);

        textViewsList10.add(mDataBinding.tvZs1);
        textViewsList10.add(mDataBinding.tvZs2);
        textViewsList10.add(mDataBinding.tvZs3);
        textViewsList10.add(mDataBinding.tvZs4);
        textViewsList10.add(mDataBinding.tvZs5);
        textViewsList10.add(mDataBinding.tvZs6);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvLocation) {//位置
            if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                showPermissionDialog();
            } else {
                Intent intent = new Intent(getActivity(), SelCityActivity.class);
                startActivityForResult(intent, 300);
            }
        }
    }

    @Override
    protected void initData() {
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            mDataBinding.tvLocation.setText(getString(R.string.no_location_permission));
        } else {
//           获取定位
            initLocation();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void getData() {
        String cityId = SPUtils.getInstance().getString("cityId", "");
        if (!TextUtils.isEmpty(cityId)) {
            mDataBinding.lineChart.setVisibility(View.VISIBLE);
            getCity24(cityId);
            getCity7(cityId);
            getZl(cityId);
            getTomorrowZl(cityId);
            getLife(cityId);
        } else {
            mDataBinding.lineChart.setVisibility(View.GONE);
        }
    }

    private void getCity24(String cityId) {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("cityId", cityId);
        /*   stringStringHashMap.put("token", "008d2ad9197090c5dddc76f583616606");*/
        OkhttpUtils.getInstance().postRequest("whapi/json/alicityweather/forecast24hours", stringStringHashMap, new OkhttpUtils.HttpCallBack() {
            @Override
            public void onSusscess(String data) {
                Hours24Bean cityInfoBean = new Gson().fromJson(data, Hours24Bean.class);
                if (cityInfoBean.getCode() == 0) {
                    List<Hours24Bean.DataBean.HourlyBean> hourList = cityInfoBean.getData().getHourly();
                    if (!hourList.isEmpty()) {
                        //当前气温
                        int temp = (int) hourList.get(0).getTemp();
                        mDataBinding.tvTemperature.setText(temp + "℃");
                        if (!TextUtils.isEmpty(hourList.get(0).getCondition())) {
                            //当前天气
                            mDataBinding.tvWeather.setText(hourList.get(0).getCondition());
                        }
                        if (hourList.get(0).getConditionId() < 8) {
                            if (hourList.get(0).getHour() > 6 && hourList.get(0).getHour() <= 18) {
                                //白天晴
                                mDataBinding.ivWeatherImg.setImageResource(R.drawable.aqingtian);
                            } else {
                                //夜間晴
                                mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayewan);
                            }
                        } else if (hourList.get(0).getConditionId() >= 8 && hourList.get(0).getConditionId() <= 14) {
                            //阴，多云
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayintian);
                        } else if (hourList.get(0).getConditionId() >= 15 && hourList.get(0).getConditionId() <= 23) {
                            //雨天
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayutian);
                        } else if (hourList.get(0).getConditionId() >= 24 && hourList.get(0).getConditionId() <= 25) {
                            //雪天
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayutian);
                        } else if (hourList.get(0).getConditionId() >= 26 && hourList.get(0).getConditionId() <= 35) {
                            //用多云
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayintian);
                        } else if (hourList.get(0).getConditionId() >= 26 && hourList.get(0).getConditionId() <= 36) {
                            //用多云
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayintian);
                        } else if (hourList.get(0).getConditionId() >= 37 && hourList.get(0).getConditionId() <= 48) {
                            //用雨天
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayutian);
                        } else if (hourList.get(0).getConditionId() >= 49 && hourList.get(0).getConditionId() <= 50) {
                            //用多云
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayintian);
                        } else if (hourList.get(0).getConditionId() >= 51 && hourList.get(0).getConditionId() <= 57) {
                            //用雨天
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayutian);
                        } else if (hourList.get(0).getConditionId() >= 58 && hourList.get(0).getConditionId() <= 63) {
                            //用多云
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayintian);
                        } else if (hourList.get(0).getConditionId() >= 64 && hourList.get(0).getConditionId() <= 70) {
                            //用雨天
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayutian);
                        } else if (hourList.get(0).getConditionId() >= 71 && hourList.get(0).getConditionId() <= 77) {
                            //用多云
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayintian);
                        } else if (hourList.get(0).getConditionId() >= 78 && hourList.get(0).getConditionId() <= 79) {
                            //用雨天
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayutian);
                        } else if (hourList.get(0).getConditionId() >= 80 && hourList.get(0).getConditionId() <= 85) {
                            //用多云
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayintian);
                        } else {
                            mDataBinding.ivWeatherImg.setImageResource(R.drawable.ayutian);
                        }
                        switch (hourList.get(0).getWindDir()) {//
                            case "N":
                                mDataBinding.tvFlSd.setText("北风" + hourList.get(0).getWindlevel() + getString(R.string.unit_level) + " " + getString(R.string.humidity) + hourList.get(0).getHumidity() + getString(R.string.unit_percent));
                                break;
                            case "NNE":
                            case "NE":
                            case "ENE":
                                mDataBinding.tvFlSd.setText("东北风" + hourList.get(0).getWindlevel() + getString(R.string.unit_level) + " " + getString(R.string.humidity) + hourList.get(0).getHumidity() + getString(R.string.unit_percent));
                                break;
                            case "E":
                                mDataBinding.tvFlSd.setText("东风" + hourList.get(0).getWindlevel() + getString(R.string.unit_level) + " " + getString(R.string.humidity) + hourList.get(0).getHumidity() + getString(R.string.unit_percent));
                                break;
                            case "ESE":
                            case "SE":
                            case "SSE":
                                mDataBinding.tvFlSd.setText("东南风" + hourList.get(0).getWindlevel() + getString(R.string.unit_level) + " " + getString(R.string.humidity) + hourList.get(0).getHumidity() + getString(R.string.unit_percent));
                                break;
                            case "S":
                                mDataBinding.tvFlSd.setText("南风" + hourList.get(0).getWindlevel() + getString(R.string.unit_level) + " " + getString(R.string.humidity) + hourList.get(0).getHumidity() + getString(R.string.unit_percent));
                                break;
                            case "SSW":
                            case "SW":
                            case "WSW":
                                mDataBinding.tvFlSd.setText("西南风" + hourList.get(0).getWindlevel() + getString(R.string.unit_level) + " " + getString(R.string.humidity) + hourList.get(0).getHumidity() + getString(R.string.unit_percent));
                                break;
                            case "W":
                                mDataBinding.tvFlSd.setText("西风" + hourList.get(0).getWindlevel() + getString(R.string.unit_level) + " " + getString(R.string.humidity) + hourList.get(0).getHumidity() + getString(R.string.unit_percent));
                                break;
                            case "WNW":
                            case "NW":
                            case "NNW":
                                mDataBinding.tvFlSd.setText("西北风" + hourList.get(0).getWindlevel() + getString(R.string.unit_level) + " " + getString(R.string.humidity) + hourList.get(0).getHumidity() + getString(R.string.unit_percent));
                                break;
                            case "Calm":
                                mDataBinding.tvFlSd.setText("微风" + hourList.get(0).getWindlevel() + getString(R.string.unit_level) + " " + getString(R.string.humidity) + hourList.get(0).getHumidity() + getString(R.string.unit_percent));
                                break;
                            case "Whirlwind":
                                mDataBinding.tvFlSd.setText("旋转风" + hourList.get(0).getWindlevel() + getString(R.string.unit_level) + " " + getString(R.string.humidity) + hourList.get(0).getHumidity() + getString(R.string.unit_percent));
                                break;
                        }
                        List<Float> tempList = new ArrayList<>();
                        for (int i = 0; i < hourList.size(); i++) {
                            Hours24Bean.DataBean.HourlyBean hourlyBean = hourList.get(i);
                            tempList.add(hourlyBean.getTemp());
                            if (i > 0) {
                                textViewsList7.get(i).setText(hourlyBean.getHour() + "");
                            }
                            textViewsList8.get(i).setText(hourlyBean.getCondition());
                            textViewsList9.get(i).setText(hourlyBean.getWindlevel() + getString(R.string.unit_level));
                        }
                        set24HourData(tempList);
                    }
                }

            }

            @Override
            public void onFail(String meg) {
                Log.e("sssssss", meg);
            }
        });
    }

    private ZlBean zlBean;

    private void getZl(String cityId) {
        //获取空气质量
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("cityId", cityId);
        /*   stringStringHashMap.put("token", "008d2ad9197090c5dddc76f583616606");*/
        OkhttpUtils.getInstance().postRequest("whapi/json/alicityweather/aqi", stringStringHashMap, new OkhttpUtils.HttpCallBack() {
            @Override
            public void onSusscess(String data) {
                zlBean = new Gson().fromJson(data, ZlBean.class);
                ZlBean.DataBean.AqiBean aqi = zlBean.getData().getAqi();
                if (aqi.getValue() > 0 && aqi.getValue() <= 50) {
                    //优
                    mDataBinding.tvKqzl.setBackgroundResource(R.drawable.you_bg);
                    mDataBinding.ivTodayKqzl.setImageResource(R.drawable.you);
                    mDataBinding.tvKqzl.setText(getString(R.string.optimal) + " " + aqi.getValue());
                } else if (aqi.getValue() > 50 && aqi.getValue() <= 100) {
                    //良
                    mDataBinding.tvKqzl.setBackgroundResource(R.drawable.liang_bg);
                    mDataBinding.ivTodayKqzl.setImageResource(R.drawable.liang);
                    mDataBinding.tvKqzl.setText(getString(R.string.good) + " " + aqi.getValue());
                } else if (aqi.getValue() > 100 && aqi.getValue() <= 150) {
                    //轻度污染
                    mDataBinding.tvKqzl.setBackgroundResource(R.drawable.cha_bg);
                    mDataBinding.ivTodayKqzl.setImageResource(R.drawable.cha);
                    mDataBinding.tvKqzl.setText(getString(R.string.poor) + " " + aqi.getValue());
                } else if (aqi.getValue() > 150 && aqi.getValue() <= 200) {
                    //中度污染
                    mDataBinding.tvKqzl.setBackgroundResource(R.drawable.cha_bg);
                    mDataBinding.ivTodayKqzl.setImageResource(R.drawable.cha);
                    mDataBinding.tvKqzl.setText(getString(R.string.poor) + " " + aqi.getValue());
                } else if (aqi.getValue() > 200 && aqi.getValue() <= 300) {
                    //重度污染
                    mDataBinding.tvKqzl.setBackgroundResource(R.drawable.cha_bg);
                    mDataBinding.ivTodayKqzl.setImageResource(R.drawable.cha);
                    mDataBinding.tvKqzl.setText(getString(R.string.poor) + " " + aqi.getValue());
                } else if (aqi.getValue() > 300) {
                    //严重污染
                    mDataBinding.tvKqzl.setBackgroundResource(R.drawable.cha_bg);
                    mDataBinding.ivTodayKqzl.setImageResource(R.drawable.cha);
                    mDataBinding.tvKqzl.setText(getString(R.string.poor) + " " + aqi.getValue());
                }
            }

            @Override
            public void onFail(String meg) {
                Log.e("sssssss", meg);
            }
        });
    }

    private void getTomorrowZl(String cityId) {
        //获取空气质量
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("cityId", cityId);
        /*   stringStringHashMap.put("token", "008d2ad9197090c5dddc76f583616606");*/
        OkhttpUtils.getInstance().postRequest("/whapi/json/alicityweather/aqiforecast5days", stringStringHashMap, new OkhttpUtils.HttpCallBack() {
            @Override
            public void onSusscess(String data) {
                AqiForecastRespBean aqiForecastRespBean = new Gson().fromJson(data, AqiForecastRespBean.class);
                AqiForecastRespBean.AqiForecastBean aqi = aqiForecastRespBean.getData().getAqiForecast().get(1);
                if (aqi.getValue() > 0 && aqi.getValue() <= 50) {
                    //优
                    mDataBinding.ivTomorrowKqzl.setImageResource(R.drawable.you);
                } else if (aqi.getValue() > 50 && aqi.getValue() <= 100) {
                    //良
                    mDataBinding.ivTomorrowKqzl.setImageResource(R.drawable.liang);
                } else if (aqi.getValue() > 100 && aqi.getValue() <= 150) {
                    //轻度污染
                    mDataBinding.ivTomorrowKqzl.setImageResource(R.drawable.cha);
                } else if (aqi.getValue() > 150 && aqi.getValue() <= 200) {
                    //中度污染
                    mDataBinding.ivTomorrowKqzl.setImageResource(R.drawable.cha);
                } else if (aqi.getValue() > 200 && aqi.getValue() <= 300) {
                    //重度污染
                    mDataBinding.ivTomorrowKqzl.setImageResource(R.drawable.cha);
                } else if (aqi.getValue() > 300) {
                    //严重污染
                    mDataBinding.ivTomorrowKqzl.setImageResource(R.drawable.cha);
                }
            }

            @Override
            public void onFail(String meg) {
                Log.e("sssssss", meg);
            }
        });
    }

    private void getLife(String cityId) {
        //生活指数
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("cityId", cityId);
        OkhttpUtils.getInstance().postRequest("whapi/json/alicityweather/index", stringStringHashMap, new OkhttpUtils.HttpCallBack() {
            @Override
            public void onSusscess(String json) {
                LiveIndexRespBean resp = new Gson().fromJson(json, LiveIndexRespBean.class);
                List<LiveIndexRespBean.IndexItemBean> indexItemBeanList = new ArrayList<>();
                for (List<LiveIndexRespBean.IndexItemBean> value : resp.getData().getLiveIndex().values()) {
                    indexItemBeanList.addAll(value);
                }
                mDataBinding.tvZs1.setText(indexItemBeanList.get(0).getStatus());
                mDataBinding.tvZs2.setText(indexItemBeanList.get(4).getStatus());
                mDataBinding.tvZs3.setText(indexItemBeanList.get(3).getStatus());
                mDataBinding.tvZs4.setText(indexItemBeanList.get(2).getStatus());
                mDataBinding.tvZs5.setText(indexItemBeanList.get(8).getStatus());
                mDataBinding.tvZs6.setText(indexItemBeanList.get(5).getStatus());
            }

            @Override
            public void onFail(String meg) {
                Log.e("sssssss", meg);
            }
        });
    }

    private void getCity7(String cityId) {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("cityId", cityId);
        /*   stringStringHashMap.put("token", "008d2ad9197090c5dddc76f583616606");*/
        OkhttpUtils.getInstance().postRequest("whapi/json/alicityweather/forecast15days", stringStringHashMap, new OkhttpUtils.HttpCallBack() {
            @Override
            public void onSusscess(String data) {
                XQ7Bean xq7Bean = new Gson().fromJson(data, XQ7Bean.class);
                if (xq7Bean.getCode() == 0) {
                    List<XQ7Bean.DataBean.ForecastBean> forecast = xq7Bean.getData().getForecast();
                    List<Float> tempDayList = new ArrayList<>();
                    List<Float> tempNightList = new ArrayList<>();
                    for (int i = 0; i < forecast.size(); i++) {
                        XQ7Bean.DataBean.ForecastBean forecastBean = forecast.get(i);
                        float tempDay = forecastBean.getTempDay();
                        float tempNight = forecastBean.getTempNight();
                        int temD = (int) tempDay;
                        int temN = (int) tempNight;
                        tempDayList.add(tempDay);
                        tempNightList.add(tempNight);
                        textViewsList1.get(i).setText(forecastBean.getConditionDay());
                        textViewsList2.get(i).setText(forecastBean.getConditionNight());
                        textViewsList6.get(i).setText(TimeUtils.millis2String(TimeUtils.string2Millis(forecastBean.getPredictDate(), new SimpleDateFormat("yyyy-MM-dd")), new SimpleDateFormat("MM/dd")));
                        if (i == 1) {
                            mDataBinding.tvTodayTemperature.setText(temN + "~" + temD + getString(R.string.unit_temperature));
                            mDataBinding.tvTodayWeather.setText(forecastBean.getConditionDay());

                            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
                            long sunrise = TimeUtils.string2Millis(forecastBean.getSunrise(), dateFormat);
                            long sunset = TimeUtils.string2Millis(forecastBean.getSunset(), dateFormat);
                            long moonrise = TimeUtils.string2Millis(forecastBean.getMoonrise(), dateFormat);
                            long moonset = TimeUtils.string2Millis(forecastBean.getMoonset(), dateFormat);
                            String moonPhase = forecastBean.getMoonphase();
                            mDataBinding.tvSunrise.setText(TimeUtils.millis2String(sunrise, timeFormat));
                            mDataBinding.tvSunset.setText(TimeUtils.millis2String(sunset, timeFormat));
                            mDataBinding.tvMoonrise.setText(TimeUtils.millis2String(moonrise, timeFormat));
                            mDataBinding.tvMoonset.setText(TimeUtils.millis2String(moonset, timeFormat));
                            SimpleDateFormat sdf = new SimpleDateFormat("HH时mm分", Locale.CHINA);
                            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));   // 关键
                            mDataBinding.tvDayLength.setText(sdf.format(new Date(sunset - sunrise)));
                            mDataBinding.tvDayLength.setText(TimeUtils.millis2String(sunset - sunrise, sdf));

                            if (moonPhase.equals("New")) {
                                moonPhase = "新月";
                            } else if (moonPhase.equals("WaxingCrescent")) {
                                moonPhase = "蛾眉月";
                            } else if (moonPhase.equals("First")) {
                                moonPhase = "上弦月";
                            } else if (moonPhase.equals("WaxingGibbous")) {
                                moonPhase = "盈凸月";
                            } else if (moonPhase.equals("Full")) {
                                moonPhase = "满月";
                            } else if (moonPhase.equals("WaningGibbous")) {
                                moonPhase = "亏凸月";
                            } else if (moonPhase.equals("LastQuarter")) {
                                moonPhase = "下弦月";
                            } else if (moonPhase.equals("WaningCrescent")) {
                                moonPhase = "残月";
                            } else {
                                moonPhase = "残月";
                            }
                            mDataBinding.tvMoonPhase.setText(moonPhase);
                        }
                        if (i == 2) {
                            mDataBinding.tvTomorrowTemperature.setText(temN + "~" + temD + getString(R.string.unit_temperature));
                            mDataBinding.tvTomorrowWeather.setText(forecastBean.getConditionDay());
                        }

                        if (i > 2) {
                            textViewsList5.get(i).setText(TimeUtils.millis2String(TimeUtils.string2Millis(forecastBean.getPredictDate(), new SimpleDateFormat("yyyy-MM-dd")), new SimpleDateFormat("EEE")));
                        }

                        textViewsList3.get(i).setText(forecastBean.getWindDirDay());
                        textViewsList4.get(i).setText(forecastBean.getWindLevelDay() + getString(R.string.unit_level));

                        if (forecastBean.getConditionIdDay() == 0) {
                            //白天晴
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_1);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_1);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_1);
                            }
                        } else if (forecastBean.getConditionIdDay() == 1) {
                            //白天多云
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_4);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_4);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_4);
                            }
                        } else if (forecastBean.getConditionIdDay() == 2) {
                            //白天阴
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_3);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_3);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_3);
                            }
                        } else if (forecastBean.getConditionIdDay() == 3) {
                            //白天阵雨
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_12);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_12);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_12);
                            }
                        } else if (forecastBean.getConditionIdDay() == 4) {
                            //白天雷阵雨
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_11);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_11);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_11);
                            }
                        } else if (forecastBean.getConditionIdDay() == 5) {
                            //白天雷阵雨冰雹
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_15);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_15);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_15);
                            }
                        } else if (forecastBean.getConditionIdDay() == 6) {
                            //白天雨夹雪
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_15);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_15);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_15);
                            }
                        } else if (forecastBean.getConditionIdDay() == 7) {
                            //白天小雨
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_5);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_5);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_5);
                            }
                        } else if (forecastBean.getConditionIdDay() == 8) {
                            //白天中雨
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_6);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_6);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_6);
                            }
                        } else if (forecastBean.getConditionIdDay() == 9) {
                            //白天大雨
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_7);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_7);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_7);
                            }
                        } else if (forecastBean.getConditionIdDay() == 10) {
                            //白天暴雨
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_8);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_8);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_8);
                            }
                        } else if (forecastBean.getConditionIdDay() == 13) {
                            //白天阵雪
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_23);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_23);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_23);
                            }
                        } else if (forecastBean.getConditionIdDay() == 14) {
                            //白天小雪
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_13);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_13);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_13);
                            }
                        } else if (forecastBean.getConditionIdDay() == 15 || forecastBean.getConditionIdDay() == 16) {
                            //白天大雪
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_14);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_14);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_14);
                            }
                        } else if (forecastBean.getConditionIdDay() == 17) {
                            //白天暴雪
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_25);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_25);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_25);
                            }
                        } else if (forecastBean.getConditionIdDay() == 18) {
                            //白天雾
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_19);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_19);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_19);
                            }
                        } else if (forecastBean.getConditionIdDay() == 19) {
                            //白天冻雨
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_20);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_20);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_20);
                            }
                        } else if (forecastBean.getConditionIdDay() == 20) {
                            //白天沙尘暴
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_21);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_21);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_21);
                            }
                        } else if (forecastBean.getConditionIdDay() == 29) {
                            //白天浮尘
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_17);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_17);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_17);
                            }
                        } else if (forecastBean.getConditionIdDay() == 45) {
                            //白天霾
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_18);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_18);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_18);
                            }
                        } else {
                            imgList1.get(i).setBackgroundResource(R.drawable.weather_1);
                            if (i == 1) {
                                mDataBinding.ivTodayWeather.setImageResource(R.drawable.weather_1);
                            }
                            if (i == 2) {
                                mDataBinding.ivTomorrowWeather.setImageResource(R.drawable.weather_1);
                            }
                        }

                        if (forecastBean.getConditionIdNight() == 2 || forecastBean.getConditionIdNight() == 3) {
                            //夜间阴
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_2);
                        } else if (forecastBean.getConditionIdNight() == 4) {
                            //夜间雷阵雨
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_11);
                        } else if (forecastBean.getConditionIdNight() == 5) {
                            //夜间雷阵雨冰雹
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_15);
                        } else if (forecastBean.getConditionIdNight() == 6) {
                            //白天雨夹雪
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_15);
                        } else if (forecastBean.getConditionIdNight() == 7) {
                            //白天小雨
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_5);
                        } else if (forecastBean.getConditionIdNight() == 8) {
                            //白天中雨
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_6);
                        } else if (forecastBean.getConditionIdNight() == 9) {
                            //白天大雨
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_7);
                        } else if (forecastBean.getConditionIdNight() == 10) {
                            //白天暴雨
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_8);
                        } else if (forecastBean.getConditionIdNight() == 14) {
                            //白天小雪
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_13);
                        } else if (forecastBean.getConditionIdNight() == 15 || forecastBean.getConditionIdNight() == 16) {
                            //白天大雪
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_14);
                        } else if (forecastBean.getConditionIdNight() == 17) {
                            //白天暴雪
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_25);
                        } else if (forecastBean.getConditionIdNight() == 18) {
                            //夜间阴
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_3);
                        } else if (forecastBean.getConditionIdNight() == 19) {
                            //夜间冻雨
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_20);
                        } else if (forecastBean.getConditionIdNight() == 20) {
                            //白天沙尘暴
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_21);
                        } else if (forecastBean.getConditionIdNight() == 30) {
                            //夜间晴
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_2);
                        } else if (forecastBean.getConditionIdNight() == 31) {
                            //夜间多云
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_3);
                        } else if (forecastBean.getConditionIdNight() == 32) {
                            //夜间雾
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_19);
                        } else if (forecastBean.getConditionIdNight() == 33) {
                            //夜间阵雨.
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_6);
                        } else if (forecastBean.getConditionIdNight() == 34) {
                            //夜间阵雪
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_23);
                        } else if (forecastBean.getConditionIdNight() == 35) {
                            //夜间浮尘
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_17);
                        } else if (forecastBean.getConditionIdNight() == 36) {
                            //夜间沙尘暴
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_21);
                        } else if (forecastBean.getConditionIdNight() == 46) {
                            //夜间霾
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_19);
                        } else {
                            //夜间晴
                            imgList2.get(i).setBackgroundResource(R.drawable.weather_2);
                        }
                    }
                    setData(tempDayList, tempNightList);
                }

            }

            @Override
            public void onFail(String meg) {
                Log.e("sssssss", meg);
            }
        });
    }

    private void setData(List<Float> tempDayList, List<Float> tempNightList) {
        // 1. 模拟数据（16天温度）
        List<Entry> maxEntries = new ArrayList<>();
        List<Entry> minEntries = new ArrayList<>();

        for (int i = 0; i < 16; i++) {
            maxEntries.add(new Entry(i, tempDayList.get(i)));
            minEntries.add(new Entry(i, tempNightList.get(i)));
        }

        // 2. 配置数据集
        LineDataSet maxDataSet = new LineDataSet(maxEntries, "");
        LineDataSet minDataSet = new LineDataSet(minEntries, "");
        configureDataSet(maxDataSet, true); // 最高温在上方显示数值
        configureDataSet(minDataSet, false); // 最低温在下方显示数值

        // 3. 绑定数据
        LineData lineData = new LineData(maxDataSet, minDataSet);
        mDataBinding.lineChart.setData(lineData);

        // 4. 配置图表样式
        configureChartAppearance(mDataBinding.lineChart);

        mDataBinding.lineChart.invalidate();
    }

    private void set24HourData(List<Float> tempList) {
        // 1. 模拟数据（24小时）
        List<Entry> maxEntries = new ArrayList<>();

        for (int i = 0; i < 25; i++) {
            maxEntries.add(new Entry(i, tempList.get(i)));
        }

        // 2. 配置数据集
        LineDataSet maxDataSet = new LineDataSet(maxEntries, "");
        configureDataSet(maxDataSet, true); // 最高温在上方显示数值

        // 3. 绑定数据
        LineData lineData = new LineData(maxDataSet);
        mDataBinding.lineChartHour.setData(lineData);

        // 4. 配置图表样式
        configureChartAppearance(mDataBinding.lineChartHour);

        mDataBinding.lineChartHour.invalidate();
    }

    private void configureDataSet(LineDataSet dataSet, boolean isMax) {
        int color = Color.parseColor("#65A0F7");
        dataSet.setColor(color);
        dataSet.setLineWidth(1f);
        dataSet.setCircleColor(color);
        dataSet.setCircleHoleColor(Color.WHITE);
        dataSet.setCircleRadius(6f);
        dataSet.setCircleHoleRadius(4f);
        dataSet.setDrawCircleHole(true);
        dataSet.setMode(LineDataSet.Mode.LINEAR);

        // 数值标签配置
        dataSet.setDrawValues(true);
        dataSet.setValueTextColor(Color.parseColor("#EF8F1D"));
        dataSet.setValueTextSize(12f);
        dataSet.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return String.valueOf((int) value);
            }
        });
    }

    private void configureChartAppearance(LineChart chart) {
        // 隐藏所有坐标轴和网格
        XAxis xAxis = chart.getXAxis();
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawLabels(false); // 隐藏横轴标签

        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setDrawAxisLine(false);
        leftAxis.setDrawGridLines(false);
        leftAxis.setDrawLabels(false); // 隐藏纵轴标签

        chart.getAxisRight().setEnabled(false);

        // 其他配置
        chart.getDescription().setEnabled(false);
        chart.getLegend().setEnabled(false); // 隐藏图例
        chart.setTouchEnabled(false); // 禁用触摸交互
        chart.setDrawGridBackground(false);
        chart.setViewPortOffsets(30f, 0f, 30f, 30f); // 调整边距
    }

    private LocationManager mLocationMgr; // 声明一个定位管理器对象


    private boolean isLocationEnable = false; // 定位服务是否可用

    public void initLocation() {
        mLocationMgr = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria(); // 创建一个定位准则对象
        criteria.setAccuracy(Criteria.ACCURACY_FINE); // 设置定位精确度
        criteria.setAltitudeRequired(true); // 设置是否需要海拔信息
        criteria.setBearingRequired(true); // 设置是否需要方位信息
        criteria.setCostAllowed(true); // 设置是否允许运营商收费
        criteria.setPowerRequirement(Criteria.POWER_LOW); // 设置对电源的需求
        // 获取定位管理器的最佳定位提供者
        String bestProvider = mLocationMgr.getBestProvider(criteria, true);
        if (bestProvider != null && mLocationMgr.isProviderEnabled(bestProvider)) { // 定位提供者当前可用
            beginLocation(bestProvider); // 开始定位
            isLocationEnable = true;
        } else { // 定位提供者暂不可用
            Toast.makeText(getActivity(), "请开启定位服务", Toast.LENGTH_SHORT).show();
            isLocationEnable = false;
        }
    }

    private void beginLocation(String method) {

        // 检查当前设备是否已经开启了定位功能

        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)

                != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getActivity(), "请授予定位权限并开启定位功能", Toast.LENGTH_SHORT).show();
            return;

        }

        // 设置定位管理器的位置变更监听器

        mLocationMgr.requestLocationUpdates(method, 300, 0, mLocationListener);

        // 获取最后一次成功定位的位置信息

        Location location = mLocationMgr.getLastKnownLocation(method);
        if (location != null) {
            setLocation(location); // 显示定位结果文本
        }


    }
    // 定义一个位置变更监听器

    private LocationListener mLocationListener = new LocationListener() {

        @Override

        public void onLocationChanged(Location location) {
            if (location != null) {
                setLocation(location); // 显示定位结果文本
            }
        }

        @Override

        public void onProviderDisabled(String arg0) {
        }

        @Override

        public void onProviderEnabled(String arg0) {
        }

        @Override

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        }

    };

    private void setLocation(Location location) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        String city = getCityName(latitude, longitude);
        if (!TextUtils.isEmpty(city)) {
            mLocationMgr.removeUpdates(mLocationListener);
        }
        getData();

    }

    private String getCityName(double lat, double lon) {
        // 方案1：使用Android原生Geocoder
        if (Geocoder.isPresent()) {
            Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
            try {
                List<Address> addresses = geocoder.getFromLocation(lat, lon, 1);
                if (!addresses.isEmpty()) {
                    String city = addresses.get(0).getLocality();
                    mDataBinding.tvLocation.setText(city);
                    SPUtils.getInstance().put("cityId", CityUtils.getCityId(city));
                    return city;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    private void showPermissionDialog() {
        PermissionExplainDialog dialog = new PermissionExplainDialog(mContext);
        dialog.setContent(getString(R.string.get_location_permission));
        PermissionUtils.permission(Manifest.permission.ACCESS_FINE_LOCATION).explain(new PermissionUtils.OnExplainListener() {
            @Override
            public void explain(@NonNull UtilsTransActivity activity, @NonNull List<String> denied, @NonNull ShouldRequest shouldRequest) {
                dialog.show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        shouldRequest.start(true);
                    }
                }, 500);
            }
        }).callback(new PermissionUtils.SimpleCallback() {
            @Override
            public void onGranted() {
                dialog.dismiss();
                initLocation();
            }

            @Override
            public void onDenied() {
                dialog.dismiss();
            }
        }).request();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 300 && resultCode == 200 && data != null) {
            CityBean bean = (CityBean) data.getSerializableExtra("bean");
            SPUtils.getInstance().put("cityId", bean.getCityId());
            mDataBinding.tvLocation.setText(bean.getCityName());
            getData();
        }
    }
}